package com.chee.plant.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;

import org.apache.mina.transport.socket.nio.SocketConnector;

import com.chee.plant.client.ClientHandler.Callback;
import com.chee.plant.logic.PlantCmd;
import com.chee.plant.server.Message;

public class ClientDemo implements Callback {
	public static void main(String[] args){
		ClientDemo demo = new ClientDemo();
		ClientHandler handler = new ClientHandler(demo);
		ClientSupport client = new ClientSupport(handler);
        if (!client.connect(new SocketConnector(), new InetSocketAddress(9123))) {
            System.out.println("Could not connect");
        }
        InputStreamReader read = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(read);
        String line;
		try {
			line = reader.readLine();
	        while(!line.equals("quit")){
	        	if(line.equals("login")){
	        		Message msg = new Message(0,PlantCmd.Login,0);
	        		msg.addVariable("username","testdb");
	        		msg.addVariable("password","itispass");
	        		client.sendMessage(msg);
	        	}
	        	line = reader.readLine();
	        }
	        client.quit();
	        System.exit(0);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void connected() {
		// TODO Auto-generated method stub
		System.out.println("connected to server now");
	}
	@Override
	public void disconnected() {
		// TODO Auto-generated method stub
		System.out.println("server disconnected now");
	}
	@Override
	public void error(Message message) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void loggedIn() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void loggedOut() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void messageReceived(Message message) {
		// TODO Auto-generated method stub
		if(message.getCommand().equals(PlantCmd.Login)){
			if(message.getResult() == 1){
				System.out.println(message.getData("message"));
			}
		}
	}
}
